from werkzeug.security import generate_password_hash,check_password_hash
from flask_login import UserMixin
from . import db
from . import login_manager

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(64), unique=True, index=True)
    username = db.Column(db.String(64), unique=True, index=True)

# 用户信息模型
class UserInfo(db.Model):
    __tablename__ = 'userinfos'
    id = db.Column(db.Integer,primary_key=True)             # 自增长 id
    uid = db.Column(db.String(16),unique=True,index=True)   # 学号
    name = db.Column(db.String(128))                        # 姓名
    grade = db.Column(db.String(64))                        # 年级
    college = db.Column(db.String(128))                     # 学院
    specialty = db.Column(db.String(128))                   # 专业

# 用户详细信息
# class UserDetail(db.Model):
#     __tablename__ = 'userdetails'
#     id = db.Column(db.Integer,primary_key=True)             # 自增长 id
#     uid = db.Column(db.String(16),unique=True)              # 学号
#     gender = db.Column(db.Integer)                          # 性别
#     avatar = db.Column(db.String(512))                      # 头像地址
#     hometown = db.Column(db.String(128))                    # 家乡
#     birth = db.Column(db.Intenger)                          # 生日

# 课程表模型
class Courses(db.Model):
    __tablename__ = 'courses'
    id = db.Column(db.Integer,primary_key=True)
    uid = db.Column(db.String(24))
    schoolYear = db.Column(db.String(64))
    term = db.Column(db.String(24))
    cid = db.Column(db.String(64))
    cname = db.Column(db.String(64))
    period = db.Column(db.Integer)
    credit = db.Column(db.Float)
    startSection = db.Column(db.Integer)
    endSection = db.Column(db.Integer)
    startWeek = db.Column(db.Integer)
    endWeek = db.Column(db.Integer)
    dayOfWeek = db.Column(db.Integer)
    classroom = db.Column(db.String(64))
    teacher = db.Column(db.String(64))
    campus = db.Column(db.String(24))
    studyType = db.Column(db.String(24))
    studyMethod = db.Column(db.String(24))

    def to_json(self):
        result = {
        'uid':self.uid,
        'schoolYear':self.schoolYear,
        'term':self.term,
        'cid':self.cid,
        'cname':self.cname,
        'period':self.period,
        'credit':self.credit,
        'startSection':self.startSection,
        'endSection':self.endSection,
        'startWeek':self.startWeek,
        'endWeek':self.endWeek,
        'classroom':self.classroom,
        'teacher':self.teacher,
        'campus':self.campus,
        'studyType':self.studyType,
        'studyMethod':self.studyMethod
        }
        return result

# 成绩表模型
class Scores(db.Model):
    __tablename__ = 'scores'
    id = db.Column(db.Integer,primary_key=True)
    uid = db.Column(db.String(24))
    schoolYear = db.Column(db.String(64))
    term = db.Column(db.String(24))
    cid = db.Column(db.String(64))
    cname = db.Column(db.String(64))
    type = db.Column(db.String(24))
    property = db.Column(db.String(24))
    credit = db.Column(db.Float)
    score = db.Column(db.Float)
    studyMethod = db.Column(db.String(24))

    def to_json(self):
        result = {
        'uid':self.uid,
        'schoolYear':self.schoolYear,
        'term':self.term,
        'cid':self.cid,
        'cname':self.cname,
        'type':self.type,
        'property':self.property,
        'credit':self.credit,
        'score':self.score,
        'studyMethod':self.studyMethod
        }
        return result

# 考试表模型
class Exams(db.Model):
    __tablename__ = 'exams'
    id = db.Column(db.Integer,primary_key=True)
    uid = db.Column(db.String(24))
    cid = db.Column(db.String(64))
    cname = db.Column(db.String(64))
    property = db.Column(db.String(24))
    teacher = db.Column(db.String(64))
    credit = db.Column(db.Float)
    date = db.Column(db.String(64))
    time = db.Column(db.String(64))
    classroom = db.Column(db.String(64))
    type = db.Column(db.String(24))
    method = db.Column(db.String(24))
    status = db.Column(db.String(24))

    def to_json(self):
        result = {
        'uid':self.uid,
        'cid':self.cid,
        'cname':self.cname,
        'property':self.property,
        'teacher':self.teacher,
        'credit':self.credit,
        'date':self.date,
        'time':self.time,
        'classroom':self.classroom,
        'type':self.type,
        'method':self.method,
        'status':self.status
        }
        return result

# 新闻表模型
class News(db.Model):
    __tablename__ = 'news'
    nid = db.Column(db.Integer,primary_key=True)            # 新闻 id
    imgUrl = db.Column(db.String(128))                      # 图片链接
    title = db.Column(db.Text)                              # 标题
    digest = db.Column(db.Text)                             # 摘要
    content = db.Column(db.Text)                            # 内容
    type = db.Column(db.String(64))                         # 类型
    pubTime = db.Column(db.DateTime)                        # 发布时间
    publisher = db.Column(db.String(64))                    # 发布者

    def to_json_list(self):
        json_list_data = {
        'nid':self.nid,
        'imgUrl':self.imgUrl,
        'title':self.title,
        'digest':self.digest,
        'type':self.type,
        'pubTime':self.pubTime,
        'publisher':self.publisher
        }
        return json_list_data

    def to_json_detail(self):
        json_detail = {
        'nid':self.nid,
        'imgUrl':self.imgUrl,
        'title':self.title,
        'content':self.content,
        'type':self.type,
        'pubTime':self.pubTime,
        'publisher':self.publisher
        }
        return json_detail

# 管理员模型
class Administrator(UserMixin,db.Model):
    __tablename__ = 'administrators'
    id = db.Column(db.Integer,primary_key=True)
    username = db.Column(db.String(64),unique=True)
    password_hash = db.Column(db.String(128))

    @property
    def password(self):
        raise AttributeError('password is not a readable attribute')

    @password.setter
    def password(self,password):
        self.password_hash = generate_password_hash(password)

    def verify_password(self,password):
        return check_password_hash(self.password_hash,password)


@login_manager.user_loader
def load_administrator(aid):
    return Administrator.query.get(int(aid))
